/**
 *@Description 订单底账首页
 */
var orderBottomCtrl  = new XFApp.client();
XFApp.client.prototype.orderBottom = {
	successCode : "10000",
	page : null,
	urlItems : {
		queryByPageURL : basePath+"/nt/orderBottom/getPage",
		delOrderBottomURL : basePath+"/nt/orderBottom/delOrderBottom",
		importDataURL : basePath+"/nt/orderBottom/importData",
		exportDataURL: basePath+"/nt/orderBottom/exportData",
		dowonLoadURL: basePath+"/nt/orderBottom/download"
	},
	formValidate : function(){
		$("#importForm").validate({
			rules: {
				excelFile : "required"
			},
			messages: {
				excelFile: "请上传数据文件"
			}
		});
		$("#importForm").validate();
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(),
		        success: function (data, pageIndex) {
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data
								|| null == data.result
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	delOrderBottomDialog : function(id,orderBottomId){
		_this = this;
		webAlert({
            title:'提示',
            content:'确定要删除 '+orderBottomId+' 吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.delOrderBottom(id);
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	delOrderBottom : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delOrderBottomURL,
		    data:{id:id},
		    type:'post',
		    cache:false,
		    dataType:'json',
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		orderBottomCtrl.orderBottom.queryPage();
		    	}
		     },
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }
		});
	},
	importData : function(){
		var _this = this;
		var params = [];
		$("input").each(function(){
			if(!!$(this).val() && !!$(this).attr("id")){
				var param = {name:'',value:''};
				param.name=$(this).attr("id");
				param.value=$(this).val();
				params.push(param);
			}
		});
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			//基于ajaxfileUpload.js实现
			$.ajaxFileUpload({
				url : _this.urlItems.importDataURL,
				secureuri : false,
				fileElementId : 'excelFile',//file标签的id
				dataType : 'json',
				data : params,
				success: function (data, status) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示", null, 400, 200);
					}else{
						webDialogUtil.simpleAlert(data.msg, "温馨提示", null, 400, 200);
						$("#searchButton").trigger("click");
					}
				},
				error: function (data, status, e) {
					webDialogUtil.simpleAlert("处理异常", "温馨提示");
				}
			});
		});
	},
	exportData : function(){
		_this = this;
		$.ajax({
			url:_this.urlItems.exportDataURL,
			data:$('#searchForm').serialize(),
			type:'get',
			cache:false,
			success:function(data) {
				if(data.success){
					var url = _this.urlItems.dowonLoadURL+"?fileName="+data.fileName;
					$("body").append("<iframe id='file_download_frame' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
				}
			},
			error : function() {
				window.webDialogUtil.alert("提示","操作失败");
			}
		});
	},
	downloadTemplate: function(){
		var url = _this.urlItems.dowonLoadURL+"?fileName=底账导入模板.xls";
		$("body").append("<iframe id='file_download_frame' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;

		$("#searchButton").on("click",function(){
			orderBottomCtrl.orderBottom.queryPage();
		});

		$("#importButton").on("click",function(){
			if($("#importForm").validate().form()){
				orderBottomCtrl.orderBottom.importData();
			}
		});

		$("#downloadButton").on("click", function(){
			orderBottomCtrl.orderBottom.downloadTemplate();
		})

		$("#exportButton").on("click",function(){
			orderBottomCtrl.orderBottom.exportData();
		});

		orderBottomCtrl.orderBottom.queryPage();

		//表单验证
		_this.formValidate();
	}
};
$.views.converters({
	ftime : function(value) {
		return orderBottomCtrl.orderBottom.dataFormat(value) ;
	},
	fdkstatus : function(value){
		return dksCtrl.dockerstatus.get(value);
	},
	fmoney : function(value){
		if(null == value
				|| "" == value){
			return "";
		}
		return orderBottomCtrl.fmoney(value);
	}
});
$(function(){
/*	$("#editForm #orderTypeHidden").val(order.impExp);
	$("#editForm #currencyHidden").val(order.currency);
	$("#editForm #nationHidden").val(order.cosigneeCty);*/
	//币制下拉框
	dictCtrl.dictionary.load("currency","currencyHidden");
	//国家下拉框
	dictCtrl.dictionary.load("nation","nationHidden");
	orderBottomCtrl.orderBottom.event();
});